
package com.xiaocikuai.smallchunks.config;


import org.ehcache.jsr107.EhcacheCachingProvider;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.cache.expiry.Duration;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.spi.CachingProvider;


@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public javax.cache.CacheManager ehCacheManager() {
        CachingProvider cachingProvider = Caching.getCachingProvider(EhcacheCachingProvider.class.getName());
        javax.cache.CacheManager cacheManager = cachingProvider.getCacheManager();

        MutableConfiguration<Object, Object> cacheConfig = new MutableConfiguration<>()
                .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.FIVE_MINUTES))
                .setStoreByValue(true)
                .setStatisticsEnabled(true);

        cacheManager.createCache("verificationCodes", cacheConfig);

        return cacheManager;
    }

    @Bean
    public JCacheCacheManager cacheManager(javax.cache.CacheManager cacheManager) {
        return new JCacheCacheManager(cacheManager);
    }



}
